package com.example.demo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.demo.service.RegistryService;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class RegistryActivity extends AppCompatActivity {

    private EditText mUsernameEditText;
    private EditText mPassEditText;
    private EditText mNicknameEditText;
    private Button mRegistryButton;


    private String mUsername;
    private String mPassword;
    private String mNickname;

    private RegistryService mRegistryService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registry);

        ActionBar action = getSupportActionBar();
        if(action != null){
            action.hide();
        }

        mRegistryService = new RegistryService();

        mUsernameEditText = findViewById(R.id.user_name_ed);
        mPassEditText = findViewById(R.id.password_ed_zc);
        mNicknameEditText = findViewById(R.id.nickname);
        mRegistryButton = findViewById(R.id.registrynew );

        loadUsername();
        loadPassword();
        loadNickname();
        loadRegistryButton();
    }

    public void loadUsername(){
        mUsernameEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mUsername = s.toString();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public void loadPassword(){
        mPassEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mPassword = s.toString();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public void loadNickname(){
        mNicknameEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mNickname = s.toString();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public void loadRegistryButton(){
        mRegistryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRegistryService.registry(mUsername,mPassword,mNickname,new RegistryHandler());
            }
        });
    }

    private class RegistryHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            String result = msg.getData().getString("result");
            JsonObject json = JsonParser.parseString(result).getAsJsonObject();
            String resultMsg = json.get("msg").getAsString();
            int code = json.get("code").getAsInt();
            Toast.makeText(RegistryActivity.this,resultMsg,Toast.LENGTH_LONG).show();
            //跳转到登录页
            if(code==200){//通过code后端传过来的状态码，决定安卓的展示状态。
                Toast.makeText(RegistryActivity.this,"注册成功",Toast.LENGTH_LONG).show();
                Intent intent = new Intent(RegistryActivity.this, LoginActivity.class);
                startActivity(intent);
            }

        }
    }
}